package byx.container.core;

import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class Predicates {
    public static final Predicate<RegisterInfo> ALL = ri -> true;

    public static Predicate<RegisterInfo> ofId(String id) {
        return new Predicate<>() {
            @Override
            public boolean test(RegisterInfo registerInfo) {
                return registerInfo.getId().equals(id);
            }

            @Override
            public String toString() {
                return "id为" + id;
            }
        };
    }

    public static Predicate<RegisterInfo> ofType(Class<?> type) {
        return new Predicate<>() {
            @Override
            public boolean test(RegisterInfo registerInfo) {
                return type.isAssignableFrom(registerInfo.getType());
            }

            @Override
            public String toString() {
                return "类型为" + type.getCanonicalName();
            }
        };
    }

    public static Predicate<RegisterInfo> annotatedBy(Class<? extends Annotation> annotationType) {
        return new Predicate<>() {
            @Override
            public boolean test(RegisterInfo registerInfo) {
                return registerInfo.getType().isAnnotationPresent(annotationType);
            }

            @Override
            public String toString() {
                return "标注了注解" + annotationType;
            }
        };
    }
}
